home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 2 / Macwelt DVD 2.cdr / System / Entwickler-Tools / macosx / BlueJ 1.2.0 / examples / shapes / Circle.class / Circle.class (.txt)
Encoding:
Java Class File  |  2002-04-03  |  2.2 KB  |  98 lines

  1. import java.awt.geom.Ellipse2D;
  2.  
  3. public class Circle {
  4.    private int diameter = 30;
  5.    private int xPosition = 20;
  6.    private int yPosition = 60;
  7.    private String color = "blue";
  8.  
  9.    public Circle() {
  10.       this.draw();
  11.    }
  12.  
  13.    public void moveRight() {
  14.       this.moveHorizontal(20);
  15.    }
  16.  
  17.    public void moveLeft() {
  18.       this.moveHorizontal(-20);
  19.    }
  20.  
  21.    public void moveUp() {
  22.       this.moveVertical(-20);
  23.    }
  24.  
  25.    public void moveDown() {
  26.       this.moveVertical(20);
  27.    }
  28.  
  29.    public void moveHorizontal(int distance) {
  30.       this.erase();
  31.       this.xPosition += distance;
  32.       this.draw();
  33.    }
  34.  
  35.    public void moveVertical(int distance) {
  36.       this.erase();
  37.       this.yPosition += distance;
  38.       this.draw();
  39.    }
  40.  
  41.    public void slowMoveHorizontal(int distance) {
  42.       int delta;
  43.       if (distance < 0) {
  44.          delta = -1;
  45.          distance = -distance;
  46.       } else {
  47.          delta = 1;
  48.       }
  49.  
  50.       for(int i = 0; i < distance; ++i) {
  51.          this.erase();
  52.          this.xPosition += delta;
  53.          this.draw();
  54.       }
  55.  
  56.    }
  57.  
  58.    public void slowMoveVertical(int distance) {
  59.       int delta;
  60.       if (distance < 0) {
  61.          delta = -1;
  62.          distance = -distance;
  63.       } else {
  64.          delta = 1;
  65.       }
  66.  
  67.       for(int i = 0; i < distance; ++i) {
  68.          this.erase();
  69.          this.yPosition += delta;
  70.          this.draw();
  71.       }
  72.  
  73.    }
  74.  
  75.    public void changeSize(int newDiameter) {
  76.       this.erase();
  77.       this.diameter = newDiameter;
  78.       this.draw();
  79.    }
  80.  
  81.    public void changeColor(String newColor) {
  82.       this.color = newColor;
  83.       this.draw();
  84.    }
  85.  
  86.    private void draw() {
  87.       Canvas canvas = Canvas.getCanvas();
  88.       canvas.setForegroundColour(this.color);
  89.       canvas.fill(new Ellipse2D.Double((double)this.xPosition, (double)this.yPosition, (double)this.diameter, (double)this.diameter));
  90.       canvas.wait(10);
  91.    }
  92.  
  93.    private void erase() {
  94.       Canvas canvas = Canvas.getCanvas();
  95.       canvas.erase(new Ellipse2D.Double((double)this.xPosition, (double)this.yPosition, (double)this.diameter, (double)this.diameter));
  96.    }
  97. }
  98.